define(['app'], function(app) {
	'use strict';

	var controller = function controller($scope, Productmanager_sort_edit,GoDialog,GoPage,$stateParams,GoPicture) {
		
		$scope.scope = new Object();
		$scope.scope.cat_name = "";
		$scope.scope.cat_select = {id:"",cat_name:"请选择分类"};
		$scope.scope.parent_id = "";
		$scope.scope.p_order = "";
		$scope.scope.cat_image = new Object();//图片
		
		$scope.scope.reason_items = [{id:1,name:"质量问题"},{id:2,name:"货品和描述不符"}];
		$scope.doSelectCat1 = function(step){
			//id是值 name是显示
			var list = new Array();
			list.push({id:0,name:"无(顶级分类)"});
			$scope.catList.forEach(obj => {
				if(obj.id == undefined)
				obj.id = obj.cat_id;
				if(obj.step == step)
				list.push(obj);
			});
			GoDialog.showSelect("大分类",list,$scope.scope.cat_select,function(data){
				$scope.scope.parent_id = data.custom_cat_id;
				$scope.scope.cat_name = data.name;
				$scope.scope.cat_select = data;
			});
		}
		if($stateParams.parent_id){
			$scope.scope.parent_id = $stateParams.parent_id;
		}
		if($stateParams.custom_cat_id){
			$scope.scope.custom_cat_id = $stateParams.custom_cat_id;//父分类id
		}
		if($stateParams.add_second){
			$scope.scope.add_second = $stateParams.add_second;//父分类name
		}
		
		//初始化数据
		$scope.init_data = function(){
			Productmanager_sort_edit.cat_get(function(data) {
				$scope.catList = data.custom_cat;
			})
			if($scope.scope.custom_cat_id){
				Productmanager_sort_edit.cat_list($scope.scope.custom_cat_id,$scope.scope.add_second,function(data) {
					$scope.scope = data;
				})
			}
		}
		$scope.init_data();
		
		//提交数据
		$scope.submit = function(){
			if($scope.scope.cat_name.trim() == ""){
				GoDialog.toast("请输入分类名称！");
			}else if($scope.scope.parent_id == ""){
				GoDialog.toast("请选择上级分类！");
			}else if($scope.scope.p_order.trim() == ""){
				GoDialog.toast("请输入排序！");
			}else if(isNaN($scope.scope.p_order.trim())){
				GoDialog.toast("请输入数字排序！");
			}else if(!$scope.scope.cat_image.image){
				GoDialog.toast("请上传图片！");
			}else{
				Productmanager_sort_edit.get_edit($scope.scope.custom_cat_id,$scope.scope.parent_id,$scope.scope.p_order.trim(),$scope.scope.cat_name.trim(),$scope.scope.cat_image,function(data){
					if(data.success){
						GoDialog.toast("提交成功");
						GoPage.go_states("productmanager");
					}else{
						GoDialog.toast(data.msg);
					}
				})
			}
		}
		
		
		// 上传图片
		$scope.addStoreImg = function() {
			GoPicture.upload(function(res) {
				if(!res.success) {
					GoDialog.toast(res.msg);
					return;
				}
				$scope.scope.cat_image.id = res.result.image_id;
				$scope.scope.cat_image.image = res.result.image_src;
				GoDialog.toast("上传图片成功！");
			});
		}
		
		//图片操作
		$scope.openFileActSheet = function() {
			var buttons = new Array();
			buttons.push({ text: '删除图片', style: '' });
			buttons.push({ text: '显示大图', style: '' });

			GoDialog.sheet('图片操作', '取消', buttons, function(index) {
				if(index == 0) {
					GoDialog.confirm('删除图片', '是否要删除该图片？', function() {
						$scope.scope.cat_image = new Object();//删除图片
					})
				}else if(index == 1) {
					GoDialog.showBigImg($scope.scope.cat_image.image);//查看大图
				}
				return true;
			});
		}
		
		
	}

	controller.$inject = ['$scope', 'Productmanager_sort_edit','GoDialog','GoPage','$stateParams',"GoPicture"];
	
	return app.registerController("productmanager_sort_editctrl",controller);
});